package com.dys.dao.impl;

import com.dys.dao.EmpDao;
import com.dys.entity.Emp;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: EmpDaoImpl
 * @Description: 实现类
 * @CreateDate: 2024/09/24
 * @Version: v1.0.0
 */
public class EmpDaoImpl implements EmpDao {
    private String url = "jdbc:mysql://127.0.0.1:3306/air?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private String username = "root";
    private String password = "pinecone@mysql8";

    @Override
    public List<Emp> findAll() {

        List<Emp> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pstat = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            pstat = connection.prepareStatement("select * from emp");
            resultSet = pstat.executeQuery();
            while (resultSet.next()) {
                Integer empno = resultSet.getInt("empno");
                Integer deptno = resultSet.getInt("deptno");
                Integer mgr = resultSet.getInt("mgr");
                String ename = resultSet.getString("ename");
                String job = resultSet.getString("job");
                Double sal = resultSet.getDouble("sal");
                Double comm = resultSet.getDouble("comm");
                Date hiredate = resultSet.getDate("hiredate");
                Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
                list.add(emp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != resultSet) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != pstat) {
                try {
                    pstat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != connection) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
